package com.elinkway.infinitemovies.view;


import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Looper;
import android.os.MessageQueue.IdleHandler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.sdk.android.feedback.impl.FeedbackAPI;
import com.elinkway.infinitemovies.config.SettingManage;
import com.le123.ysdq.R;


public class ScorePopupWindow {
	private PopupWindow mSiftPopupWindow;
	private View mPopWindowView;	
	private Context context;
	private View mparentView;
	
	
	private TextView mScore;
	private TextView mAdvice;
	private TextView mNo;
	
	public ScorePopupWindow (Context context,View parentView){
		this.context = context;
		this.mparentView = parentView;
		intPopupWindow();
	}
	
	public void intPopupWindow() {
		LayoutInflater inflater = LayoutInflater.from(context);
		
		// 加载筛选弹出的Popwind模板
		mPopWindowView = inflater.inflate(R.layout.scorepop, null);
		mSiftPopupWindow = new PopupWindow(mPopWindowView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		
		mPopWindowView.getBackground().setAlpha(150);//0~255透明度值
		
		if (null != mSiftPopupWindow) {
			mSiftPopupWindow.setFocusable(true);
			// 用于POPwindow点击消失
			mSiftPopupWindow.setBackgroundDrawable(new BitmapDrawable());
			mSiftPopupWindow.setOutsideTouchable(true);
//			mSiftPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//				@Override
//				public void onDismiss() {
//					//pop不再显示
//				}
//			});
		}
		
		initView();
	}
	
	public void showPopView(){
		try {
			if(SettingManage.getInstance().isshowTimes() == 3){
				
			Looper.myQueue().addIdleHandler(new IdleHandler() {
				@Override
				public boolean queueIdle() {
					try {
						mSiftPopupWindow.showAtLocation(mparentView,Gravity.CENTER, 0, 0);
					} catch (Exception e) {
						e.printStackTrace();
					}
					return false;
				}
			});
			
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	private void initView(){
		mScore = (TextView)mPopWindowView.findViewById(R.id.go_score);
		mAdvice = (TextView)mPopWindowView.findViewById(R.id.go_advice);
		mNo = (TextView)mPopWindowView.findViewById(R.id.no);
		mScore.setOnClickListener(clickListener);
		mAdvice.setOnClickListener(clickListener);
		mNo.setOnClickListener(clickListener);
	}
	
	private OnClickListener clickListener= new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch(v.getId()){
			case R.id.no:
				if(null!=mSiftPopupWindow && mSiftPopupWindow.isShowing())
				mSiftPopupWindow.dismiss();
				
				break;
			case R.id.go_score:
				if(null!=mSiftPopupWindow && mSiftPopupWindow.isShowing())
					mSiftPopupWindow.dismiss();
				
				try {
					Uri uri = Uri.parse("market://details?id="+context.getPackageName());  
					Intent intent = new Intent(Intent.ACTION_VIEW,uri);  
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
					context.startActivity(intent);
				} catch (Exception e) {
					e.printStackTrace();
				}  
				
				break;
			case R.id.go_advice:
				if(null!=mSiftPopupWindow && mSiftPopupWindow.isShowing())
					mSiftPopupWindow.dismiss();
				FeedbackAPI.openFeedbackActivity(context);
				
				break;
			}
			
		}
	};
	
}
